//
//  MyShowIndexViewController.m
//  BosidengIphone
//
//  Created by  on 11-12-30.
//  Copyright (c) 2011年 __MyCompanyName__. All rights reserved.
//

#import "MyShowIndexViewController.h"
#import "AppDelegate.h"
#import "MyShowLoginViewController.h"
#import "MainViewController.h"
#import "CustomData.h"
#import "MyShowSelfListViewController.h"
#import "MyShowAllListViewController.h"
#import "MyShowData.h"
#import "MyShowCameraViewController.h"
#import "SyncImageView.h"
#import "RecomPageView.h"

@implementation MyShowIndexViewController
@synthesize mTableView;
@synthesize mPageControl;
@synthesize mTextView;
@synthesize recomShows;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        tableDatas = [[NSArray arrayWithObjects:[AppDelegate getLanguageString:@"show_my_show"], [AppDelegate getLanguageString:@"show_all_show"], [AppDelegate getLanguageString:@"show_at_now"], nil] retain];
        self.recomShows = [MyShowData getRecommendItems];
        self.canHanlderPageSingleTap = NO;
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

- (void)dealloc {
    [recomShows release];
    [mPageControl release];
    [mTextView release];
    [tableDatas release];
    [mTableView release];
    [super dealloc];
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    pagingScrollView.backgroundColor = [UIColor clearColor];
    mTableView.dataSource = self;
    mTableView.delegate = self;
    double count = [recomShows count];
    mPageControl.numberOfPages = ceil(count / 3.0);
    mPageControl.currentPage = 0;
    mTextView.text = [AppDelegate getLanguageString:@"show_my_show_descript"];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    self.mTableView = nil;
    self.mPageControl = nil;
    self.mTextView = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark -
#pragma mark Table view data source


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
    return [tableDatas count];
}


// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *CellIdentifier = @"ShowIndexsCellIdentifier";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        cell.textLabel.textAlignment = UITextAlignmentCenter;
        cell.textLabel.font=[UIFont systemFontOfSize:12];
        //cell.textLabel.textColor=[UIColor colorWithRed:1 green:1 blue:1 alpha:1];
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
    }
    // Configure the cell...
	NSUInteger row = [indexPath row];
    if (2 == row) {
        cell.textLabel.textColor=[UIColor colorWithRed:1.0 green:102.0f/255.0f blue:0 alpha:1];
    } else {
        cell.textLabel.textColor=[UIColor colorWithRed:1 green:1 blue:1 alpha:1];
    }
	cell.textLabel.text = [tableDatas objectAtIndex:row];
    return cell;
}

#pragma mark -
#pragma mark Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    CustomData *custom = [AppDelegate getCustom];
    NSUInteger row = [indexPath row];
	if (0 == row) {
        if (custom == nil) {
            MyShowLoginViewController *loginView = [[MyShowLoginViewController alloc] initWithNibName:@"MyShowLoginViewController" bundle:nil];
            [(MainViewController *)mainViewController pushViewController:loginView];
            [loginView release];
        } else {
            MyShowSelfListViewController *showView = [[MyShowSelfListViewController alloc] initWithNibName:@"MyShowSelfListViewController" bundle:nil];
            [(MainViewController *)mainViewController pushViewController:showView];
            [showView release];
        }
    } else if (1 == row) {
        MyShowAllListViewController *allshowView = [[MyShowAllListViewController alloc] initWithNibName:@"MyShowAllListViewController" bundle:nil];
        //allshowView.allDatas = [MyShowData getAllItems];
        [(MainViewController *)mainViewController pushViewController:allshowView];
        [allshowView release];
    } else if (2 == row) {
        if (custom == nil) {
            MyShowLoginViewController *loginView = [[MyShowLoginViewController alloc] initWithNibName:@"MyShowLoginViewController" bundle:nil];
            [(MainViewController *)mainViewController pushViewController:loginView];
            [loginView release];
        } else {
            MyShowCameraViewController *cameraView = [[MyShowCameraViewController alloc] initWithNibName:@"MyShowCameraViewController" bundle:nil];
            [(MainViewController *)mainViewController pushViewController:cameraView];
            [cameraView release];
        }
    }
}

#pragma mark -
#pragma mark Image Scroll view delegate

- (NSInteger)imageCount {
    double count = [recomShows count];
    NSInteger pageCount = ceil(count / 3.0);
    //NSLog(@"count=%.2f, pageCount=%d", count, pageCount);
    return pageCount;
}

- (UIView *)configureView:(UIView *)page atIndex:(NSInteger)index {
    if (page == nil) {
        RecomPageView *recomPage = [[[RecomPageView alloc] init] autorelease];
        recomPage.requestQueue = requestQueue;
        recomPage.recomDatas = recomShows;
        recomPage.mainViewController = mainViewController;
        page = recomPage;
    }
    [(RecomPageView *)page setPageIndex:index];
    [(RecomPageView *)page loadData];
    return page;
}

- (void)changePageIndex:(NSInteger)index {
    mPageControl.currentPage = index;
}

- (void)selectPageIndex:(NSInteger)index {
    //NSLog(@"selectPageIndex=%d", index);
}

@end
